package com.hehui.module.goods.service.impl;

import com.hehui.module.goods.dao.BaseGoodsMapper;
import com.hehui.module.goods.dao.GoodsLadderPriceMapper;
import com.hehui.module.goods.dao.GoodsPricedMapper;
import com.hehui.module.goods.entity.BaseGoods;
import com.hehui.module.goods.entity.BaseGoodsExample;
import com.hehui.module.goods.entity.GoodsLadderPrice;
import com.hehui.module.goods.service.IGoodsPricedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品控制类实现层
 */
@Service
public class GoodsPricedServiceImpl implements IGoodsPricedService {
    @Autowired
    GoodsPricedMapper goodsPricedMapper; // 商品价格mapper
    @Autowired
    BaseGoodsMapper goodsMapper; // 商品mapper
    @Autowired
    GoodsLadderPriceMapper goodsLadderPriceMapper; // 阶梯价mapper

    // 商品价格查询 带商户id 查询is_del 字段为1
    @Override
    public List<BaseGoods> findGoodsPricedAll(Long merchantId) {
        BaseGoodsExample example = new BaseGoodsExample();
        example.createCriteria().andIsDelEqualTo("1").andBgMerchantidEqualTo(merchantId);

        return goodsMapper.selectByExample(example);
    }

    // 根据商品id修改商品对应的是否阶梯价
    @Override
    public int updateGoodsIsLadderByGoodsId(Long glGoodsid) {
        BaseGoods baseGoods = new BaseGoods();
        baseGoods.setBgGoodid(glGoodsid);
        // “1” 为启用，“0” 为不起用
        baseGoods.setBgIsladder("1");
        return goodsMapper.updateByPrimaryKeySelective(baseGoods);
    }

    // 修改阶梯价 根据主键修改
    @Override
    public int updateGoodsLadderPrices(GoodsLadderPrice ladderPrice) {
        return goodsLadderPriceMapper.updateByPrimaryKeySelective(ladderPrice);
    }

    // 模糊查询带商户id
    @Override
    public List<BaseGoods> findGoodsPricedLike(BaseGoods baseGoods) {
        BaseGoodsExample example = new BaseGoodsExample();
        BaseGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andBgGoodnameLike("%"+baseGoods.getBgGoodname()+"%") // 模糊查询商品名字
        .andBgInitialLike("%"+baseGoods.getBgInitial()+"%") // 模糊查询首字母
        .andBgGoodnumLike("%"+baseGoods.getBgGoodnum()+"%") // 模糊查询商品编号
        .andBgKeywordLike("%"+baseGoods.getBgKeyword()+"%") // 模糊查询关键字
        .andBgBarcodeLike("%"+baseGoods.getBgBarcode()+"%") // 模糊查询条形码
        .andBgDefauwarehouseEqualTo(baseGoods.getBgDefauwarehouse()) // 条件是仓库
        .andBgGoodcategoryidEqualTo(baseGoods.getBgGoodcategoryid()) // 条件是分类
        .andBgIsgroundingEqualTo(baseGoods.getBgIsgrounding()) // 条件是是否上下架
        .andBgRandlabelEqualTo(baseGoods.getBgRandlabel()) // 条件是 商品标签
        .andBgTrademarkidEqualTo(baseGoods.getBgTrademarkid()) // 条件是 品牌
        .andBgLabelingEqualTo(baseGoods.getBgLabeling()) // 条件是商品标识
        .andBgLimitorderEqualTo(baseGoods.getBgLimitorder()) // 条件是订货限制
        .andBgIsladderEqualTo(baseGoods.getBgIsladder()) // 条件是是否阶梯价
        .andBgIslevelEqualTo(baseGoods.getBgIslevel()) // 条件是是否等级价
        .andBgIsspecifyEqualTo(baseGoods.getBgIsspecify()) //条件是是否指定价
        .andBgMerchantidEqualTo(baseGoods.getBgMerchantid()) // 条件是商户id
        .andIsDelEqualTo("1");// id_del 为1

        return goodsMapper.selectByExample(example);
    }
}
